public class Main {
    public static void main(String[] args) {

        int[][] costs = {{17,2,17},{16,16,5},{14,3,19}};
        Main main = new Main();
        main.minCost(costs);
    }
    //粉刷房子
    public int minCost1(int[][] costs) {
        int m=costs.length;
        int[][] dp=new int[m+1][3];
        for(int i=1;i<=m;i++){
            dp[i][0]=Math.min(dp[i-1][1],dp[i-1][2])+costs[i-1][0];
            dp[i][1]=Math.min(dp[i-1][0],dp[i-1][2])+costs[i-1][1];
            dp[i][2]=Math.min(dp[i-1][1],dp[i-1][0])+costs[i-1][2];
        }
        return Math.min(dp[m][2],Math.min(dp[m][1],dp[m][0]));
    }

    public int minCost(int[][] costs) {
        int n = costs.length;
        if (n == 0) return 0;
        //刚开始就定义好三种颜色最开始粉刷花的钱
        int red = costs[0][0], blue = costs[0][1], green = costs[0][2];
        for (int i = 1; i < n; i++) {
            int newRed = costs[i][0] + Math.min(blue, green);
            int newBlue = costs[i][1] + Math.min(red, green);
            int newGreen = costs[i][2] + Math.min(red, blue);
            red = newRed;
            blue = newBlue;
            green = newGreen;
        }
        return Math.min(red, Math.min(blue,green));
    }

    //买卖股票的最佳时机含冷冻期
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if (n == 0) return 0;
        int[][] dp = new int[n][3];
        dp[0][0]=-prices[0];
        for(int i=1;i<n;i++){
            dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]-prices[i]);
            dp[i][1]=Math.max(dp[i-1][1],dp[i-1][2]);
            dp[i][2]=dp[i-1][0]+prices[i];
        }
        return Math.max(dp[n-1][1],dp[n-1][2]);
    }
}